package io.zhile.crack.atlassian.plugins.zephyr;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Signature;
import java.security.SignatureException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;


public class EncryptionUtil {

    private PublicKey publicKey = null;

    private PrivateKey privateKey = null;


    public void generateKeys() throws IOException, NoSuchAlgorithmException, NoSuchProviderException {
        /*  51 */
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
        /*  52 */
        keyGen.initialize(1024, new SecureRandom());
        /*  53 */
        KeyPair pair = keyGen.generateKeyPair();
        /*  54 */
        this.privateKey = pair.getPrivate();
        /*  55 */
        this.publicKey = pair.getPublic();
    }


    public void generateKeys(String publicURI, String privateURI) throws IOException, NoSuchAlgorithmException, NoSuchProviderException {
        /*  66 */
        generateKeys();
        /*  67 */
        writeKeys(publicURI, privateURI);
    }


    public PublicKey getPublic() {
        /*  72 */
        return this.publicKey;
    }


    public PrivateKey getPrivate() {
        /*  77 */
        return this.privateKey;
    }


    public void writeKeys(String publicURI, String privateURI) throws IOException, FileNotFoundException {
        /*  82 */
        writePublicKey(publicURI);
        /*  83 */
        writePrivateKey(privateURI);
    }


    public void writePublicKey(String URI) throws IOException, FileNotFoundException {
        /*  88 */
        byte[] enckey = this.publicKey.getEncoded();
        /*  89 */
        FileOutputStream keyfos = new FileOutputStream(URI);
        /*  90 */
        keyfos.write(enckey);
        /*  91 */
        keyfos.close();
    }


    public void writePrivateKey(String URI) throws IOException, FileNotFoundException {
        /*  96 */
        byte[] enckey = this.privateKey.getEncoded();
        /*  97 */
        FileOutputStream keyfos = new FileOutputStream(URI);
        /*  98 */
        keyfos.write(enckey);
        /*  99 */
        keyfos.close();
    }


    public void readKeys(String publicKeyString, String privateKeyString) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
        /* 110 */
        readPublicKey(publicKeyString);
        /* 111 */
        readPrivateKey(privateKeyString);
    }


    public PublicKey readPublicKey(String URI) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
        /* 122 */
        FileInputStream keyfis = new FileInputStream(URI);
        /* 123 */
        byte[] encKey = new byte[keyfis.available()];
        /* 124 */
        keyfis.read(encKey);
        /* 125 */
        keyfis.close();
        /* 126 */
        X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(encKey);
        /* 127 */
        KeyFactory keyFactory = KeyFactory.getInstance("DSA");
        /* 128 */
        this.publicKey = keyFactory.generatePublic(pubKeySpec);
        /* 129 */
        return this.publicKey;
    }


    public PublicKey readPublicKeyFromBytes(byte[] publicKeyByteArry) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
        /* 140 */
        X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(publicKeyByteArry);
        /* 141 */
        KeyFactory keyFactory = KeyFactory.getInstance("DSA");
        /* 142 */
        this.publicKey = keyFactory.generatePublic(pubKeySpec);
        /* 143 */
        return this.publicKey;
    }


    public PrivateKey readPrivateKey(String URI) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
        /* 155 */
        FileInputStream keyfis = new FileInputStream(URI);
        /* 156 */
        byte[] encKey = new byte[keyfis.available()];


        /* 159 */
        keyfis.read(encKey);
        /* 160 */
        keyfis.close();
        /* 161 */
        PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(encKey);
        /* 162 */
        KeyFactory keyFactory = KeyFactory.getInstance("DSA");
        /* 163 */
        this.privateKey = keyFactory.generatePrivate(privKeySpec);

        /* 165 */
        return this.privateKey;
    }


    public PrivateKey readPrivateKeyFromBytes(byte[] privateKeyByteArry) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
        /* 176 */
        PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(privateKeyByteArry);
        /* 177 */
        KeyFactory keyFactory = KeyFactory.getInstance("DSA");
        /* 178 */
        this.privateKey = keyFactory.generatePrivate(privKeySpec);
        /* 179 */
        return this.privateKey;
    }


    public String sign(String message) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException, InvalidKeyException, SignatureException {
        /* 191 */
        return sign(message, this.privateKey);
    }


    public String sign(String message, String privateKeyURI) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException, InvalidKeyException, SignatureException, IOException {
        /* 204 */
        PrivateKey pk = readPrivateKey(privateKeyURI);
        /* 205 */
        return sign(message, pk);
    }


    public String sign(String message, PrivateKey privateKey) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException, InvalidKeyException, SignatureException {
        Signature dsa = Signature.getInstance("SHA/DSA");
        dsa.initSign(privateKey);
        dsa.update(message.getBytes());
        byte[] m1 = dsa.sign();
        String signature = new String(Base64Coder.encode(m1));
        return signature;
    }


    public boolean verify(String message, String signature, String publicKeyURI) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException, InvalidKeyException, SignatureException {
        PublicKey pk = readPublicKey(publicKeyURI);
        return verify(message, signature, pk);
    }


    public boolean verify(String message, String signature) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException, InvalidKeyException, SignatureException {
        if (this.publicKey == null)
            throw new InvalidKeyException("Public Key not provided.");
        return verify(message, signature, this.publicKey);
    }


    public boolean verify(String message, String signature, PublicKey publicKey) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException, InvalidKeyException, SignatureException {
        Signature dsa = Signature.getInstance("SHA/DSA");
        dsa.initVerify(publicKey);
        dsa.update(message.getBytes());
        byte[] sigDec = Base64Coder.decode(signature.toCharArray());
        return dsa.verify(sigDec);
    }
}